<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>物模型模板管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="modelTemplate-app">
    <div class="container">
        <div class="tableBar">
            <div class="tableLab">
                模型名称:<el-input v-model="conditions.propertyName" placeolder="输入模型名称" style="width: 250px" clearable></el-input>
                <el-select v-model="conditions.deviceTypeId" placeholder="选择设备类型">
                    <el-option
                            v-for="item in deviceTypeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
                <el-select v-model="conditions.modelType" placeholder="选择物模型类型">
                    <el-option
                            v-for="item in modelTypeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
                <el-button  type="primary" icon="el-icon-search" @click="handleQuery()">搜索</el-button>
            </div>
            <div class="tableLab">
                <el-button  type="success" icon="el-icon-circle-plus" @click="addClass('class')">新增物模型</el-button>
            </div>
        </div>
        <el-table  :data="pagination.tableData"  stripe  class="tableBox" >
            <el-table-column type="index" width="50" label="序号"></el-table-column>
            <el-table-column  prop="propertyName" label="物模型名称"  /></el-table-column>
            <el-table-column  label="设备类型"/>
                <template slot-scope="scope">
                    <span v-for="item in deviceTypeList" v-if="scope.row.deviceTypeId==item.id">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column  label="物模型类型"/>
                <template slot-scope="scope">
                    <span v-if="scope.row.modelType==1"><el-tag type="success">属性</el-tag></span>
                    <span v-if="scope.row.modelType==2"><el-tag type="danger">事件</el-tag></span>
                    <span v-if="scope.row.modelType==3"><el-tag type="warning">服务</el-tag></span>
                </template>
            </el-table-column>
            <el-table-column  prop="description" label="描述"  /></el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="editHandle(scope.row.id)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.counts"
                @size-change="handleSizeChange"
                :current-page.sync="pagination.page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="物模型名称：">
                <el-input v-model="classData.dialogForm.propertyName" placeholder="请输入物模型名称" maxlength="50"/>
            </el-form-item>
            <el-form-item label="设备类型：">
                <el-select v-model="classData.dialogForm.deviceTypeId" placeholder="请选择设备类型">
                    <el-option  v-for="item in deviceTypeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="物模型类型：">
                <el-select v-model="classData.dialogForm.modelType" placeholder="请选择物模型类型">
                    <el-option  v-for="item in modelTypeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="描述：">
                <el-input type="textarea" v-model="classData.dialogForm.description" placeholder="请输入描述" maxlength="50"/>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleClose()">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/modelTemplate.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#modelTemplate-app',
        data() {
            return {
                action: '',
                type: '',
                pagination: {
                    tableData: [],
                    page: 1,
                    pageSize: 10,
                    counts:0,
                },
                conditions:{
                    name:''
                },
                classData: {
                    'title': '添加物模型',
                    'dialogVisible': false,
                    dialogForm:{}
                },
                deviceTypeList: [],
                modelTypeList: [
                    {
                        'id': '1',
                        'name': '属性'
                    },
                    {
                        'id': '2',
                        'name': '事件'
                    },
                    {
                        'id': '3',
                        'name': '服务'
                    }
                ]
            }
        },
        methods: {
            async init() {
                const params = {
                    page: {
                        current: this.pagination.page,//当前页码
                        size: this.pagination.pageSize,//每页大小
                    },
                    propertyName: this.conditions.propertyName ? this.conditions.propertyName : undefined,
                    deviceTypeId: this.conditions.deviceTypeId ? this.conditions.deviceTypeId : undefined,
                    modelType: this.conditions.modelType ? this.conditions.modelType : undefined
                };
                await findModelTemplatePage(params).then(res => {
                    if (String(res.code) === '200') {
                        //console.log("==========" + JSON.stringify(res.data.records));
                        this.pagination.tableData = res.data.records;
                        this.pagination.counts = res.data.total;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            // 添加
            addClass(st) {
                this.classData.dialogForm={};//清空数据
                this.classData.title = '新增物模型';//指定对话框标题
                this.action = 'add';
                this.classData.dialogVisible = true
            },
            //根据id查询
            editHandle(st) {
                //赋值
                this.classData.title = '修改物模型';
                this.action = 'edit';
                findModelTemplateById(st).then(res => {
                    if (String(res.code) === '200') {
                        this.classData.dialogForm  = res.data;
                        this.classData.dialogForm.modelType = res.data.modelType.toString();
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                this.classData.dialogVisible = true;//打开修改框
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteModelTemplateById(id).then(res => {
                        if (res.code === 200) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                const formData = this.classData.dialogForm;
                if (this.action === 'add') {
                    addModelTemplate(formData).then(res => {
                        if (res.code === 200) {
                            this.$message.success('添加成功！')
                            this.classData.dialogVisible = false
                            this.classData.dialogForm = {};
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                } else {
                    editModelTemplate(formData).then(res => {
                        if (res.code === 200) {
                                this.$message.success('修改成功！');
                                this.classData.dialogVisible = false;
                                this.classData.dialogForm = {};
                                this.handleQuery();
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                }
            },

            //分页相关全部操作
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.id)
                })
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            //查询所有行政区
            async initDeviceTypeList(){
                await findDeviceTypeList().then(res => {
                    if (String(res.code) === '200') {
                        this.deviceTypeList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            }

        },
        created() {
            this.init();
            this.initDeviceTypeList();
        }
    })
</script>
</body>
</html>